package cn.com.nes.site.action.inventory;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.DataRes;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.module.service.inventory.PurchaseService;

@Controller("purchase")
@RequestMapping("/purchase")
public class PurchaseAction {
    @Resource
    private PurchaseService service;

    //生成订单ID
    @RequestMapping(method = RequestMethod.POST, value = "/generatePurchaseId")
    public @ResponseBody
    PageData generatePurchaseId(HttpServletRequest request,
                                HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res = new PageData();
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.generatePurchaseId(req);
        return res;
    }

    //获取订单登记列表
    @RequestMapping(method = RequestMethod.POST, value = "/getPurchaseList")
    public @ResponseBody
    PageData getPurchaseList(HttpServletRequest request,
                             HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res = new PageData();
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.getPurchaseList(req);
        return res;
    }

    //获取订单详情
    @RequestMapping(method = RequestMethod.POST, value = "/getPurchaseDetail")
    public @ResponseBody
    PageData getPurchaseDetail(HttpServletRequest request,
                               HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res = new PageData();
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.getPurchaseDetail(req);
        return res;
    }

    //保存订单信息
    @RequestMapping(method = RequestMethod.POST, value = "/savePurchaseInfo")
    public @ResponseBody
    PageData savePurchaseInfo(HttpServletRequest request,
                              HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res = new PageData();
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.savePurchaseInfo(req);
        return res;
    }

    //删除订单明细
    @RequestMapping(method = RequestMethod.POST, value = "/delPurchase")
    public @ResponseBody
    PageData delPurchase(HttpServletRequest request,
                         HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res = new PageData();
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.delPurchase(req);
        return res;
    }

    //删除订单明细
    @RequestMapping(method = RequestMethod.POST, value = "/delPurchaseGoods")
    public @ResponseBody
    PageData delPurchaseGoods(HttpServletRequest request,
                              HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res = new PageData();
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.delPurchaseGoods(req);
        return res;
    }

    //保存订单审核信息
    @RequestMapping(method = RequestMethod.POST, value = "/savePurchaseReviewerInfo")
    public @ResponseBody
    PageData savePurchaseReviewerInfo(HttpServletRequest request,
                                      HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res = new PageData();
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.savePurchaseReviewerInfo(req);
        return res;
    }

    //提交订单进行审核
    @RequestMapping(method = RequestMethod.POST, value = "/applyReviewerPurchaseInfo")
    public @ResponseBody
    PageData applyReviewerPurchaseInfo(HttpServletRequest request,
                                       HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res = new PageData();
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.applyReviewerPurchaseInfo(req);
        return res;
    }

    //订单部分提交
    @RequestMapping(method = RequestMethod.POST, value = "/updatePurchaseInfo")
    public @ResponseBody
    PageData updatePurchaseInfo(HttpServletRequest request,
                                HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res = new PageData();
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.updatePurchaseInfo(req);
        return res;
    }

    //获取厂家的订单处理列表
    @RequestMapping(method = RequestMethod.POST, value = "/getCompanyPurchaseList")
    public @ResponseBody
    PageData getCompanyPurchaseList(HttpServletRequest request,
                                    HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res = new PageData();
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.getCompanyPurchaseList(req);
        return res;
    }

    //获取厂家的订单处理明细
    @RequestMapping(method = RequestMethod.POST, value = "/getCompanyPurchaseDetail")
    public @ResponseBody
    PageData getCompanyPurchaseDetail(HttpServletRequest request,
                                      HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res = new PageData();
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.getCompanyPurchaseDetail(req);
        return res;
    }

    //获取厂家的订单处理明细
    @RequestMapping(method = RequestMethod.POST, value = "/saveCompanyPurchaseDeal")
    public @ResponseBody
    PageData saveCompanyPurchaseDeal(HttpServletRequest request,
                                     HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res = new PageData();
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.saveCompanyPurchaseDeal(req);
        return res;
    }

    //修改采购单状态
    @RequestMapping(method = RequestMethod.POST, value = "/updatePurchaseState")
    public @ResponseBody
    PageData updatePurchaseState(HttpServletRequest request,
                                     HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res = new PageData();
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.updatePurchaseState(req);
        return res;
    }

}
